package com.lqq.service.impl;

import com.lqq.entity.User;
import com.lqq.repository.UserRepository;
import com.lqq.service.UserService;
import org.hibernate.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lqq
 * @date 2018/12/6
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<User> findList() {
        return userRepository.findAll();
    }

    @Override
    public List<User> findByExample() {
        //这里提供一个Example，通过ExampleMatcher和这个例子比较，生成sql。
        User u = new User();
        u.setName("小明");

        //TODO queryByExample原理分析
        //根据例子去查找 Query By Example (QBE)
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.exact())
                .withIgnoreCase();

        Example<User> example = Example.of(u,matcher);

        return userRepository.findAll(example);
    }

    @Override
    public List<User> findByName(String name) {
        return this.userRepository.queryByName(name);
    }

    @Override
    public List<User> findBySpecification() {
        Long id = 1L;
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        PageRequest pageRequest = PageRequest.of(0, 20, sort);
        Specification<User> specification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery,
                                         CriteriaBuilder criteriaBuilder) {
                // Predicate idPredicate = criteriaBuilder.greaterThan(root.get("id"), id);
                // Predicate idInPredicate = criteriaBuilder.in(root.get("id").in(2,3));
                // Predicate userIdPredicate = criteriaBuilder.like(root.get("name"),"cuihua");
                // return criteriaBuilder.and(idPredicate, idInPredicate, userIdPredicate);

                //多个查询条件
                List<Predicate> predicateList = new ArrayList<>();

                Predicate idGreaterPre = criteriaBuilder.and(criteriaBuilder.greaterThan(root.get("id"), 1L));
                Predicate idLessPre = criteriaBuilder.and(criteriaBuilder.lessThan(root.get("id"), 10000L));
                predicateList.add(idGreaterPre);
                predicateList.add(idLessPre);
                Predicate[] predicates = new Predicate[predicateList.size()];
                return criteriaBuilder.and(predicateList.toArray(predicates));
                // return criteriaBuilder.greaterThan(root.get("id"), 1L);
            }
        };
        Page<User> dynamicPage = userRepository.findAll(specification, pageRequest);
        return dynamicPage.getContent();
    }

    /**
     * 合并两个Specification
     * @param userSpecification1
     * @param userSpecification2
     * @return
     */
    private Specification<User> combine(Specification<User> userSpecification1, Specification<User> userSpecification2){
        return userSpecification1.and(userSpecification2);
    }
}
